package uk.ac.aber.paws.client.pendrivechecker;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.*;
import javax.swing.*;

import uk.ac.aber.paws.client.config.ConfigManager;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * WarningWindow.java
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class WarningWindow extends JFrame{

	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension scrnsize = toolkit.getScreenSize();
	private String lang = "en";
	
	private JButton langFlag = null;
	private JButton okButton = null;
	private JTextArea textArea = null, textArea2 = null;
	private String driveLetters = "";
	
	
	public WarningWindow(String lang, boolean errorWindow, String driveLetters) {
	
		this.lang = lang;
		this.setTitle(PenDriveChecker.getMainConfig().getProperty("app.main.windowtitle."+lang));
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.driveLetters = driveLetters;

		removeMinMaxClose(this);
		
		Image image = null; 
	    
		this.setAlwaysOnTop(true);
		
		if (errorWindow) {
			buildErrorWindow();
		}
		else {
			buildDisclaimerWindow();
		}
		
	}
	
	public void removeMinMaxClose(Component comp)  
	  {  
	    if(comp instanceof AbstractButton)  
	    {  
	      comp.getParent().remove(comp);  
	    }  
	    if (comp instanceof Container)  
	    {  
	      Component[] comps = ((Container)comp).getComponents();  
	      for(int x = 0, y = comps.length; x < y; x++)  
	      {  
	        removeMinMaxClose(comps[x]);  
	      }  
	    }  
	  }  
	
	public void applyLanguage() {
		
		if (lang.equals("en")) {
			lang = "cy";
		}
		else {
			lang = "en";
		}
		
		try {
			this.setTitle(PenDriveChecker.getMainConfig().getProperty("app.main.windowtitle."+lang));
			
			Image image = ImageIO.read(getClass().getResource("/uk/ac/aber/dal/images/"+lang+".gif"));
			langFlag.setIcon(new ImageIcon(image));
	
			okButton.setText(PenDriveChecker.getMainConfig().getProperty("app.main.lang.ok."+lang));
			textArea.setText(PenDriveChecker.getMainConfig().getProperty("app.main.message1."+lang));
			textArea2.setText(PenDriveChecker.getMainConfig().getProperty("app.main.message2."+lang));
		}
		catch (IOException e) { 
			e.printStackTrace();         
		}
		
		
	}
	
	private void buildErrorWindow() {
	
		JPanel panel = new JPanel();
		
		this.setSize(200,200);
		this.setLocation(((int)scrnsize.getWidth()/2)-200, ((int)scrnsize.getHeight()/2)-50);
		panel.add(new JLabel("You need to pass the program executable path as a parameter!"));
		

		this.add(panel);
		
	}
	
	private void buildDisclaimerWindow() {
		
		JPanel panel = new JPanel();
		
		panel.setLayout(new GridBagLayout());
		
		this.setSize(400,250);
		this.setLocation(((int)scrnsize.getWidth()/2)-200, ((int)scrnsize.getHeight()/2)-300);
		
		panel.setBackground(Color.WHITE);
		
		textArea = new JTextArea(PenDriveChecker.getMainConfig().getProperty("app.main.message1."+lang));
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=2;
		gbc.insets=new Insets(10,10,10,10);
		
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		panel.add(textArea,gbc);
		
		gbc.gridy=1;
		gbc.gridx=0;
		gbc.gridwidth=1;
		textArea2 = new JTextArea(driveLetters);
		textArea2.setLineWrap(true);
		textArea2.setWrapStyleWord(true);
		panel.add(textArea2,gbc);
		
		gbc.gridy=1;
		gbc.gridx=1;
		gbc.gridwidth=1;
		BufferedImage image = null;
		
		try {           
			image = ImageIO.read(getClass().getResource("/uk/ac/aber/paws/client/pendrivechecker/pendrive.png")); 	        
		}
		catch (IOException e) { 
			e.printStackTrace();         
		} 
		
		panel.add(new JLabel(new ImageIcon(image)),gbc);

		gbc.gridy=2;
		gbc.gridx=0;
		gbc.gridwidth=2;
		textArea2 = new JTextArea(PenDriveChecker.getMainConfig().getProperty("app.main.message2."+lang));
		textArea2.setColumns(30);
		textArea2.setLineWrap(true);
		textArea2.setWrapStyleWord(true);
		panel.add(textArea2,gbc);
	
		JPanel controlPanel = new JPanel();
		controlPanel.setBackground(Color.WHITE);
		gbc = new GridBagConstraints();
		controlPanel.setLayout(new GridBagLayout());
		
		okButton = new JButton(PenDriveChecker.getMainConfig().getProperty("app.main.lang.ok."+lang));
		gbc.gridx=0;
		gbc.gridy=3;
		okButton.addActionListener(new WarningButtonListener("OK",this));
		
		panel.add(okButton,gbc);
		
		this.add(panel);
		
	}
	
}
